<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style type="text/css">

        #banner{
            margin: 100px auto;
            position: relative;
            width: 813px;
            height: 320px;
        }

        #banner>img{
            height: 100%;
            width: 100%;
            cursor: pointer;
        }

        #banner>div{
            position: absolute;
            bottom: 20px;
            right: 20px;
        }

        #banner>div>span{
            display: block;
            width: 10px;
            height: 10px;
            float: left;
            border: #333333 solid 2px;
            border-radius: 50%;
            background-color: #717987;
            margin-left: 10px;
            cursor: pointer;
        }

        #banner>div>span:first-child{
            background-color: white;
        }

    </style>
</head>
<body>

<div id="banner">
    <img src="img/banner-img-0.jpg"/>
    <div>
        <span></span>
        <span></span>
        <span></span>
        <span></span>
        <span></span>
    </div>
</div>

<script type="text/javascript" src="js/jquery-1.12.3.js" ></script>
<script type="text/javascript">

    var $bannerImg = $("#banner").children("img");
    var $bannerPoints = $("#banner>div");
   var prePoint = 0;//记录上一个被点击的小点
    $bannerPoints.delegate("span", "click mouseenter", function () {
        clearInterval(bannerInterval);//每次点击时，先停止轮播
        var $2 = $(this);
        if (prePoint !== $2.index()) { //防止鼠标移入时 用户点击了 重复两次走相同的逻辑赋值
            currentPoint = $2.index();//赋值-- 关联到图片的循环设置
            $2.css("background-color", "white").parent().children(":eq(" + prePoint + ")").css("background-color", "#717987");
            $bannerImg.attr("src", "img/banner-img-" + $2.index() + ".jpg");
            prePoint = $2.index(); //记录上一个点击的小点
        }
    });
    $bannerPoints.delegate("span", "mouseleave", function () { //鼠标离开时 继续轮播
        startBannerInterval();
    });
    var bannerInterval;
    var currentPoint = 0;
    var pointSize = $bannerPoints.children().length - 1;//用来重置小点的判断
    function startBannerInterval() {
        var children = $bannerPoints.find("span");
        bannerInterval = setInterval(function () {
            children.eq(prePoint).css("background-color", "#717987");
            prePoint = currentPoint; //赋值-- 关联到图片的循环设置
            if (currentPoint > pointSize) {
                currentPoint = 0;
                prePoint = 0;
            }
            children.eq(currentPoint).css("background-color", "white");
            $bannerImg.attr("src", "img/banner-img-" + currentPoint + ".jpg");
            currentPoint++;
        }, 2000);
    }
    startBannerInterval();//开始轮播

</script>

</body>
</html>
